package com.kabigon.tiny.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.tiny.utils
 * @data 2022/11/30 23:07
 */
@Slf4j
public class FtpUtils {

    public static void main(String[] args) {
        Ftp ftp = null;
        ByteArrayOutputStream bos = null;
        BufferedOutputStream os = null;
        try  {
            ftp = new Ftp("192.168.31.132", Ftp.DEFAULT_PORT, "xubiao", "999225");
            //进入远程目录
            ftp.cd("/upload");
            bos = new ByteArrayOutputStream();
            ftp.download("/upload", "1130.jpeg", bos);
            /* ByteArrayInputStream bis = IoUtil.toStream(bos); */
            File file = FileUtil.file("D:\\xxxx.jpeg");
            os = FileUtil.getOutputStream(file);
            os.write(bos.toByteArray());
            //file.
            os.flush();
        } catch (Exception e) {
            log.error("ftp异常：{}", e.getMessage());
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (bos != null) {
                    bos.close();
                }
                Objects.requireNonNull(ftp).close();

            } catch (IOException e) {
                log.error("ftp关闭失败：{}", e.getMessage());
            }
        }
    }

}
